package com.isoftstone.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.isoftstone.entity.Role;
import com.isoftstone.entity.Users;
import com.isoftstone.service.RoleService;
import com.isoftstone.service.UserService;



@Controller
@RequestMapping("users")
public class UserController {
	
	@Autowired
	private UserService service;
	
	@Autowired
	private RoleService roleService;

	@RequestMapping("userList")
	public String getAllUsers(ModelMap modelMap){
	// TODO Auto-generated method stub
		
		List<Users> userList=service.getAllUserList();
		List<Role> roleList=roleService.getAllRoleList();
		
		modelMap.addAttribute("userList",userList);
		modelMap.addAttribute("roleList",roleList);
		return "UserList";
		
	}
	@RequestMapping("insertUser")
	@ResponseBody
	public boolean insertUser(String user_id,String user_name,String role_id){
		
			Users users=new Users();
			users.setUser_id(user_id);
			users.setUser_name(user_name);
			Role role=new Role();
			role.setRole_id(role_id);
			users.setRole(role);
			
			boolean flag = service.insertUser(users);
			return flag;	
	}

	@RequestMapping("getUserByUserName")
	@ResponseBody
	public Users getUserByUserName(String user_id){
		Users user = service.getUserByUserName(user_id);
		return user;
	}
	
	@RequestMapping("editUser")
	@ResponseBody
	public boolean editUser(String user_id,String user_name,String role_id){
		
			
			Users users=new Users();
			users.setUser_id(user_id);
			users.setUser_name(user_name);
			Role role=new Role();
			role.setRole_id(role_id);
			users.setRole(role);
			
			boolean flag = service.editUser(users);
			return flag;		
	}
	
	@RequestMapping("deleteUser")
	@ResponseBody
	public boolean deletUser(String user_id){
		
			
			boolean flag = service.deleteUser(user_id);
			return flag;		
	}


}
